package top.scsoul.voes.framework.web.service;

import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.crypto.password.PasswordEncoder;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import top.scsoul.voes.common.core.domain.model.RoleVo;
import top.scsoul.voes.common.core.domain.model.UserVo;
import top.scsoul.voes.common.core.domain.entity.Permission;
import top.scsoul.voes.common.core.domain.entity.Role;
import top.scsoul.voes.common.core.domain.entity.User;
import top.scsoul.voes.system.mapper.*;
import top.scsoul.voes.system.service.RoleService;
import top.scsoul.voes.system.service.UserService;

import java.util.ArrayList;
import java.util.List;

@Slf4j
@Transactional
@Service
public class DataInitService {


    @Autowired
    PermissionMapper permissionMapper;

    @Autowired
    UserMapper userMapper;
    @Autowired
    UserService userService;
    @Autowired
    PasswordEncoder passwordEncoder;
    @Autowired
    RoleMapper roleMapper;
    @Autowired
    RoleService roleService;

    @Autowired
    UserRoleMapper userRoleMapper;
    @Autowired
    RolePermissionMapper rolePermissionMapper;

    private volatile Integer progress = 0;

    public synchronized void init() {
        clear();
        initBasePermission();
        initBaseUser();
    }

    public void clear() {
        setProgress(0);
        log.info("清空RBAC中间表");
        userRoleMapper.clearTable();
        rolePermissionMapper.clearTable();

        setProgress(20);
        log.info("清空permission表");
        permissionMapper.clearTable();
        setProgress(30);

        log.info("清空role表");
        roleMapper.clearTable();
        setProgress(40);
        log.info("清空user表");
        userMapper.clearTable();

    }


    public void initBasePermission() {
        permissionMapper.clearTable();
        setProgress(50);
        ArrayList<Permission> permissions = new ArrayList<>();
        //用户控制器
        permissions.add(new Permission("/api/user/users", "system:user:list", "用户管理"));
        permissions.add(new Permission("/api/user/get/{id}", "system:user:query", "查询用户"));
        permissions.add(new Permission("/api/user/add", "system:user:insert", "添加用户"));
        permissions.add(new Permission("/api/user/delete", "system:user:delete", "删除用户"));
        permissions.add(new Permission("/api/user/deletes", "system:user:deletes", "批量删除用户"));
        permissions.add(new Permission("/api/user/update", "system:user:update", "修改用户"));
        permissions.add(new Permission("/api/user/resetPassword", "system:user:resetPsw", "重置密码"));
        permissions.add(new Permission("/api/user/export", "system:user:export", "导出用户"));
        permissions.add(new Permission("/api/user/import", "system:user:import", "导入用户"));
        permissions.add(new Permission("/api/user/getImportExcel", "system:user:getImportExcel", "用户导入模板"));
        permissions.add(new Permission("/api/user/self/update", "system:user:selfUpdate", "修改个人信息"));
        permissions.add(new Permission("/api/user/self/resetPsw", "system:user:selfResetPsw", "修改个人密码"));


        //角色控制器
        permissions.add(new Permission("/api/role/roles", "system:role:list", "角色管理"));
        permissions.add(new Permission("/api/role/rolesDict", "system:role:dict", "角色字典"));
        permissions.add(new Permission("/api/role/get/{id}", "system:role:query", "查询角色"));
        permissions.add(new Permission("/api/role/delete", "system:role:delete", "删除角色"));
        permissions.add(new Permission("/api/role/add", "system:role:insert", "添加角色"));
        permissions.add(new Permission("/api/role/update", "system:role:update", "修改角色"));
        permissions.add(new Permission("/api/role/export", "system:role:export", "导出角色"));
        //权限控制器
        permissions.add(new Permission("/api/permission/permissions", "system:permission:list", "权限管理"));
        permissions.add(new Permission("/api/permission/permissionDict", "system:permission:dict", "权限字典"));
        permissions.add(new Permission("/api/permission/get/{id}", "system:permission:query", "查询权限"));
        permissions.add(new Permission("/api/permission/add", "system:permission:add", "添加权限"));
        permissions.add(new Permission("/api/permission/delete", "system:permission:delete", "删除权限"));
        permissions.add(new Permission("/api/permission/update", "system:permission:update", "修改权限"));
        permissions.add(new Permission("/api/permission/export", "system:permission:export", "导出权限"));
        //字典控制器
        permissions.add(new Permission("/api/dict/queryByTypeCode", "system:dict:query", "查询字典"));
        permissions.add(new Permission("/api/dict/subjectDict", "system:dict:subjectDict", "考试科目字典"));

        //系统
        permissions.add(new Permission("/api/system/reset", "system:system:reset", "系统重置"));
        //permissions.add(new Permission("/api/system/resetProgress", "resetSystemProgress", "系统重置进度条"));


        //考试
        permissions.add(new Permission("/api/examination/examinations", "examination:examination:list", "考试管理"));
        permissions.add(new Permission("/api/examination/add", "examination:examination:insert", "添加考试"));
        permissions.add(new Permission("/api/examination/delete", "examination:examination:delete", "删除考试"));
        permissions.add(new Permission("/api/examination/deletes", "examination:examination:deletes", "批量删除考试"));
        permissions.add(new Permission("/api/examination//get/{id}", "examination:examination:query", "查询考试"));
        permissions.add(new Permission("/api/examination/update", "examination:examination:update", "修改考试"));
        permissions.add(new Permission("/api/examination/export", "examination:examination:export", "导出考试"));
        permissions.add(new Permission("/api/examination/randomQuestions", "examination:examination:randomQuestions", "随机抽题"));
        permissions.add(new Permission("/api/examination/testPage", "examination:examination:getTestPage", "查看电子试卷"));
        permissions.add(new Permission("/api/examination/examInfo", "examination:examination:examInfo", "查看待考考试"));

        permissions.add(new Permission("/api/examination/examRegistration", "examination:examination:examRegistration", "报名考试"));
        permissions.add(new Permission("/api/examination/myExam", "examination:examination:myExam", "查询已报考试"));
        permissions.add(new Permission("/api/examination/querySelfExamRegistration", "examination:examination:querySelfExamRegistration", "查询报名信息"));
        permissions.add(new Permission("/api/examination/answerSheetTestPaper", "examination:examination:answerSheetTestPaper", "获取答题试卷"));
        permissions.add(new Permission("/api/examination/submitAnswerSheet", "examination:examination:submitAnswerSheet", "考生交卷"));
        permissions.add(new Permission("/api/examination/getOnlineExameeInfo", "examination:examination:getOnlineExameeInfo", "参考考生信息"));
        permissions.add(new Permission("/api/examination/exameeOnliner", "examination:examination:exameeOnliner", "发送考试心跳"));
        permissions.add(new Permission("/api/examination/getOnlineExameeHeat", "examination:examination:getOnlineExameeHeat", "接收考试心跳"));
        permissions.add(new Permission("/api/examination/markingExamPapers", "examination:examination:markingExamPapers", "阅卷"));
        permissions.add(new Permission("/api/examination/announceResults", "examination:examination:announceResults", "公布成绩"));
        permissions.add(new Permission("/api/examination/selfScoreQuery", "examination:examination:selfScoreQuery", "成绩查询"));
        permissions.add(new Permission("/api/examination/selfTestPaperQuery", "examination:examination:selfTestPaperQuery", "考生考后查看试卷"));

        //题库
        permissions.add(new Permission("/api/questionBank/questionBanks", "examination:questionBank:list", "题库管理"));
        permissions.add(new Permission("/api/questionBank/get/{id}", "examination:questionBank:query", "查询题库"));
        permissions.add(new Permission("/api/questionBank/add", "examination:questionBank:insert", "添加题库"));
        permissions.add(new Permission("/api/questionBank/update", "examination:questionBank:update", "修改题库"));
        permissions.add(new Permission("/api/questionBank/delete", "examination:questionBank:delete", "删除题库"));
        permissions.add(new Permission("/api/questionBank/deletes", "examination:questionBank:deletes", "批量删除题库"));
        permissions.add(new Permission("/api/questionBank/searchCount", "examination:questionBank:searchCount", "查询题库数量"));

        setProgress(60);

        if (permissions.size() > 0) {
            permissions.forEach(p -> {
                permissionMapper.insert(p);
            });
            log.info("{}条数据插入成功！", permissions.size());
        }
        setProgress(70);
    }

    public void initBaseUser() {
        setProgress(80);
        UserVo userVo = new UserVo();
        userVo.setUsername("admin");
        userVo.setName("admin");
        userVo.setPassword("admin");

        List<Permission> permissions = permissionMapper.queryAll(null);
        ArrayList<Integer> ids = new ArrayList<>();
        permissions.forEach(p -> {
            ids.add(p.getId());
        });
        setProgress(90);

        RoleVo roleVo = new RoleVo();
        roleVo.setRoleKeyword("ROLE_ADMIN");
        roleVo.setRoleDesc("管理员");
        roleVo.setPermissionIds(ids);
        Role role = roleService.insert(roleVo);


        ids.clear();
        ids.add(role.getId());
        userVo.setRoleIds(ids);
        User user = userService.insert(userVo);
        log.info("管理员用户：{}", user);

        RoleVo ks = new RoleVo();
        ks.setRoleDesc("考生");
        ks.setRoleKeyword("ROLE_EXAMINEE");
        roleService.insert(ks);

        setProgress(100);

    }

    public Integer getProgress() {
        return progress;
    }

    public void setProgress(Integer progress) {
        this.progress = progress;
    }

}
